﻿using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;

namespace ThreadUse
{
    class Program
    {
        public static void Main(string[] args)
        {
            //for (int i = 0; i < 20; i++)
            //{
            //ThreadPool.QueueUserWorkItem(DoWork);//线程池，减少频繁创建线程的开销
        //    Thread thread = new Thread(new ThreadStart(Work));
        //    thread.Start();
        //    Thread thread1 = new Thread(new ThreadStart(Work));
        //    thread1.Start();
        //    Thread thread2 = new Thread(new ThreadStart(Work));
        //    thread2.Start();
        //    Thread parameterizedThread = new Thread(new ParameterizedThreadStart(DoWorkWithParam));
        //    parameterizedThread.Start("test");
        //}
        //int[] threads = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 };
        //Parallel.ForEach(threads, i =>
        //     {
        //         Console.WriteLine("loop:{0},thread id:{1}", i, Thread.CurrentThread.ManagedThreadId);
        //         Console.WriteLine("Work thread{0}: still alive and working.", Thread.CurrentThread.ManagedThreadId);
        //             Thread.Sleep(1000);
        //             Console.WriteLine("Work thread{0}: finished working.", Thread.CurrentThread.ManagedThreadId);
        //     });
            Parallel.For(0, 20, i =>
            {
                Console.WriteLine("loop:{0},thread id:{1}", i, Thread.CurrentThread.ManagedThreadId);
                Console.WriteLine("Work thread{0}: still alive and working.", Thread.CurrentThread.ManagedThreadId);
                //Thread.Sleep(1000);
                Console.WriteLine("Work thread{0}: finished working.", Thread.CurrentThread.ManagedThreadId);
            });
                //Console.WriteLine("Work thread{0}: still alive and working.", Thread.CurrentThread.ManagedThreadId);
                ////Thread.Sleep(1000);
                //Console.WriteLine("Work thread{0}: finished working.", Thread.CurrentThread.ManagedThreadId);
            

            Console.ReadKey();
        }

        public static void DoWork(Object o)
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("loop:{0},thread id:{1}", i, Thread.CurrentThread.ManagedThreadId);
            }
            Console.WriteLine("Work thread{0}: still alive and working.", Thread.CurrentThread.ManagedThreadId);
            //Thread.Sleep(1000);
            Console.WriteLine("Work thread{0}: finished working.", Thread.CurrentThread.ManagedThreadId);
        }


        public static void Work()
        {
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("loop:{0},thread id:{1}", i, Thread.CurrentThread.ManagedThreadId);
            }


            Console.WriteLine("Work thread{0}: still alive and working.", Thread.CurrentThread.ManagedThreadId);
            //Thread.Sleep(1000);
            Console.WriteLine("Work thread{0}: finished working.", Thread.CurrentThread.ManagedThreadId);
        }
        public static void DoWorkWithParam(object obj)
        {
            string msg = (string)obj;
            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("loop:{0},thread id:{1}", i, Thread.CurrentThread.ManagedThreadId);

            }
            Console.WriteLine("Work thread{0}: still alive and working.", Thread.CurrentThread.ManagedThreadId);
            //Thread.Sleep(1000);
            Console.WriteLine("Work thread{0}: finished working.", Thread.CurrentThread.ManagedThreadId);
        }
    }
}

